package com.yxq.mdt.app.modules.bottomsheet;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.widgets.tagview.Tag;
import com.yxq.mdt.app.widgets.tagview.TagView;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author L
 * @time 2019-12-05
 * @describe 个性标签
 */
public class PersonalPersonalityTagsSheetDialogFragment extends BottomSheetDialogFragment {
    Unbinder unbinder;
    @BindView(R.id.dialog_tv_number)
    TextView dialogTvNumber;
    @BindView(R.id.dialog_tv_number_love)
    TextView dialogTvNumberLove;
    @BindView(R.id.dialog_tv_save)
    TextView dialogTvSave;
    @BindView(R.id.recyclerView)
    TagView mTagView;
    @BindView(R.id.tag_view)
    TagView mTagViewHobby;
    String[] keys = new String[]{"暖男", "单身狗", "闷骚", "阳光", "风趣幽默", "靠谱", "专一", "讲义气", "感性", "吃货", "逗逼", "理性", "大叔", "敢爱敢性", "宅", "选择恐惧症", "理想主义", "文艺", "霸道", "内向", "热血"};
    HashMap<String, String> selectMap;
    String[] keys2 = new String[]{"看电影", "听歌", "运动健身", "旅行", "玩游戏", "睡觉", "吃鸡", "汽车", "王者荣耀", "篮球", "酒", "LOL", "读书", "爬山", "摄影", "买买买", "角色扮演", "足球", "潮流", "逛街", "烘焙", "画画"};
    HashMap<String, String> selectMap2;

    public PersonalPersonalityTagsSheetDialogFragment(String tags) {

        selectMap = new HashMap<>();
        selectMap2 = new HashMap<>();
        //初始化选中数据
        if (tags != null) {
            String[] data = tags.split(",");
            for (String s : data) {
                for (int i = 0; i < keys.length; i++) {
                    if (s.equals(keys[i])) {
                        selectMap.put(i + "", s);
                        break;
                    }
                }
                for (int i = 0; i < keys2.length; i++) {
                    if (s.equals(keys2[i])) {
                        selectMap2.put(i + "fuck", s);
                        break;
                    }
                }
            }
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.personal_personality_tags_bottom_sheet_fragment_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        initViews();
        return view;
    }

    private void initViews() {

        dialogTvNumber.setText(String.format("(%d/%d)", selectMap.size(), 6));
        dialogTvNumberLove.setText(String.format("(%d/%d)", selectMap2.size(), 6));
        initTags();
        initTags2();
    }

    private void initTags() {
        mTagView.removeAllTags();
        for (int i = 0; i < keys.length; i++) {
            Tag tag = new Tag(keys[i]);
            if (selectMap.get(i + "") != null) {
                tag.tagTextColor = getResources().getColor(R.color.white);
                tag.layoutColor = getResources().getColor(R.color.color_font_red);
                tag.layoutColorPress = getResources().getColor(R.color.color_font_red);
            } else {
                tag.tagTextColor = getResources().getColor(R.color.color_font_black_666);
                tag.layoutColor = getResources().getColor(R.color.bg_gray);
                tag.layoutColorPress = getResources().getColor(R.color.gray);
            }
            tag.radius = 60f;
            tag.tagTextSize = 14f;
            tag.layoutBorderSize = 1f;
            tag.layoutBorderColor = getResources().getColor(R.color.bg_gray);
            tag.isDeletable = false;
            mTagView.addTag(tag);
        }
        mTagView.setOnTagClickListener((position, tag) -> {
            if (selectMap.containsKey(position + "")) {
                selectMap.remove(position + "");
            } else {
                if (selectMap.size() >= 6) {
                    ToastUtils.showShort("每一项最多只能选择6个哦");
                    return;
                }
                selectMap.put(position + "", keys[position]);
            }
            dialogTvNumber.setText(String.format("(%d/%d)", selectMap.size(), 6));
            initTags();
        });
    }

    private void initTags2() {
        mTagViewHobby.removeAllTags();
        for (int i = 0; i < keys2.length; i++) {
            Tag tag = new Tag(keys2[i]);
            if (selectMap2.get(i + "fuck") != null) {
                tag.tagTextColor = getResources().getColor(R.color.white);
                tag.layoutColor = getResources().getColor(R.color.color_font_red);
                tag.layoutColorPress = getResources().getColor(R.color.color_font_red);
            } else {
                tag.tagTextColor = getResources().getColor(R.color.color_font_black_666);
                tag.layoutColor = getResources().getColor(R.color.bg_gray);
                tag.layoutColorPress = getResources().getColor(R.color.gray);
            }
            tag.radius = 60f;
            tag.tagTextSize = 14f;
            tag.layoutBorderSize = 1f;
            tag.layoutBorderColor = getResources().getColor(R.color.bg_gray);
            tag.isDeletable = false;
            mTagViewHobby.addTag(tag);
        }
        mTagViewHobby.setOnTagClickListener((position, tag) -> {
            if (selectMap2.containsKey(position + "fuck")) {
                selectMap2.remove(position + "fuck");
            } else {
                if (selectMap2.size() >= 6) {
                    ToastUtils.showShort("每一项最多只能选择6个哦");
                    return;
                }
                selectMap2.put(position + "fuck", keys2[position]);
            }
            dialogTvNumberLove.setText(String.format("(%d/%d)", selectMap2.size(), 6));
            initTags2();
        });
    }

    PersonalPersonalityTagsSheetDialogFragmentInterface personalPersonalityTagsSheetDialogFragmentInterface;

    public void setPersonalPersonalityTagsSheetDialogFragmentInterface(PersonalPersonalityTagsSheetDialogFragmentInterface personalPersonalityTagsSheetDialogFragmentInterface) {
        this.personalPersonalityTagsSheetDialogFragmentInterface = personalPersonalityTagsSheetDialogFragmentInterface;
    }

    public interface PersonalPersonalityTagsSheetDialogFragmentInterface {
        void checked(HashMap<String, String> data);
    }

    @OnClick(R.id.dialog_tv_save)
    public void onViewClicked() {

        if (personalPersonalityTagsSheetDialogFragmentInterface != null) {
            HashMap<String, String> data = new HashMap<>();
            for (String s : selectMap.keySet()) {
                data.put(s, selectMap.get(s));
            }
            for (String s : selectMap2.keySet()) {
                data.put(s, selectMap2.get(s));
            }
            personalPersonalityTagsSheetDialogFragmentInterface.checked(data);
        }
        dismiss();
    }
}
